// 节流-防止连续点击-开始运行一次
function throttle(handler,wait){
    var lastTime=0;
    return function (e) {
        //arguments[0]===e
        var nowTime = new Date().getTime();
        if(nowTime-lastTime>wait){
            handler.apply(this,arguments);
            lastTime=nowTime;
        }
    }
}

// 节流-搜索ajax请求-最后运行一次
function debounce(handler,delay) {
    var timer=null;
    return function () {
        var _self=this,_arg=arguments;
        clearTimeout(timer);
        timer=setTimeout(function () {
            handler.apply(_self,_arg)
        },delay)
    }
}

//类型判断
function typeOf(obj) {
    const toString = Object.prototype.toString;
    const map = {
        '[object Boolean]':'boolean',
        '[object Number]':'number',
        '[object String]':'string',
        '[object Function]':'function',
        '[object Array]':'array',
        '[object Date]':'date',
        '[object RegExp]':'regExp',
        '[object Undefined]':'undefined',
        '[object Null]':'null',
        '[object Object]':'object'
    };

    return map[toString.call(obj)]
}


// 深拷贝
function deepClone(obj) {
    //判断拷贝的要进行深拷贝的是数组还是对象，是数组的话进行数组拷贝，对象的话进行对象拷贝
    var objClone = Array.isArray(obj) ? [] : {};
    //进行深拷贝的不能为空，并且是对象或者是
    if (obj && typeof obj === "object") {
        for (key in obj) {
            if (obj.hasOwnProperty(key)) {
                if (obj[key] && typeof obj[key] === "object") {
                    objClone[key] = deepClone(obj[key]);
                } else {
                    objClone[key] = obj[key];
                }
            }
        }
    }
    return objClone;
}


/**
 * 
 * 函数化编程compose
 * 
 * 用例
var greet =function(name) { return 'h1:'+name}
var exclaim = function(statement) { return statement.toUpperCase() + '!' }
var transform  = function (str) {
    return str.replace(/[dD]/,'DDDDD')
}
var welcome= compose(greet,exclaim,transform);
console.log(welcome('dot'));
 *  */
function compose() {
    var fns = [].slice.call(arguments);
    return function(initialArg) {
        var res = initialArg
        for(var i=fns.length-1;i>-1;i--) {
            res = fns[i](res)
        }
        return res;
    }
}

//es6的compose
function compose2(...funcs) {
    console.log(funcs)
    if(funcs.length===0) {
        console.log('funcs的长度为0时：',funcs)
        return arg=>arg
    }
    if(funcs.length===1) {
        console.log('funcs的长度为1时：',funcs)
        return funcs[0]
    }

    return funcs.reduce((a, b) => (...args) => a(b(...args)))
}




/**
 * 函数柯里化
 * 参数复用
 * 延迟运行
 * 扁平化
 * 
 * 
 * 用例
 * function add(num1,num2) {
            return num1 + num2;
        }

        var curriedAdd1 = curry(add,5);
        console.log(curriedAdd1(2))

        var curriedAdd2 = curry(add,4,8);
        console.log(curriedAdd2())
 */
function curry(fn) {
    var args = [].slice.call(arguments,1);
    return function() {
        var newArgs = args.concat([].slice.call(arguments));
        return fn.apply(null,newArgs)
    }
}